package Operation;

import demo1.Book;
import demo1.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    public void work(BookList bookList) {
        int currentSize = bookList.getUsedSize();
        if(currentSize == 0){
            System.out.println("书架为空，不能删除！");
            return;
        }
        System.out.println("删除图书！");
        System.out.println("请输入你要删除的图书书名！");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                index = i;
                 break;
            }

        }
        if(index == -1) {
            System.out.println("没有你要删除的这本书！");
            return;
        }
        for (int i = index; i < currentSize - 1; i++) {
            Book book = bookList.getBook(i+1);
            bookList.setBook(i,book);
        }
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功！");
    }
}
